/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
 */
package td.domain;

import java.util.ArrayList;
import java.util.Date;

import com.googlecode.objectify.annotation.Ignore;

/**
 * Any statistical objects that are groupings (of other objects). Right now
 * these are: Topics, Tags & Categories.
 * 
 * @author Artem Shamsutdinov
 * 
 */
public abstract class StatisticalGrouping extends Statistical {

	private static final long serialVersionUID = 2279575699648934490L;

	public enum GroupType {
		TOPIC, //
		TAG, //
		CATEGORY; //
	}

	@Ignore
	protected Thinker moderator;

	// Statistics
	private Date lastThoughtAddedDate;
	private Long numThoughts = 0L;

	@Ignore
	public ArrayList<Thought> thoughts = new ArrayList<Thought>();

	public abstract String getTypeForUrl();

	public static GroupType getTypeFromUrl(String type) {
		if (GroupType.TOPIC.name().equals(type)) {
			return GroupType.TOPIC;
		} else if (GroupType.TAG.name().equals(type)) {
			return GroupType.TAG;

		} else if (GroupType.CATEGORY.name().equals(type)) {
			return GroupType.CATEGORY;

		} else {
			throw new RuntimeException(
					"Unsupported StatisticalGrouping GroupType: " + type);
		}
	}

	public Thinker getModerator() {
		return moderator;
	}

	public void setModerator(Thinker moderator) {
		this.moderator = moderator;
	}

	public ArrayList<Thought> getThoughts() {
		return this.thoughts;
	}

	public Long getNumThoughts() {
		return numThoughts;
	}

	public void setNumThoughts(Long numThoughts) {
		this.numThoughts = numThoughts;
	}

	public Date getLastThoughtAddedDate() {
		return lastThoughtAddedDate;
	}

	public void setLastThoughtAddedDate(Date lastThoughtAddedDate) {
		this.lastThoughtAddedDate = lastThoughtAddedDate;
	}

	@Override
	public int compareTo(Statistical otherStats) {
		if (otherStats instanceof StatisticalGrouping) {
			StatisticalGrouping otherGrouping = (StatisticalGrouping) otherStats;
			switch (getComparisonType()) {
			case LAST_THOUGHT_ADDED_DATE:
				return otherGrouping.lastThoughtAddedDate
						.compareTo(lastThoughtAddedDate);
			case NUMBER_OF_THOUGHTS:
				return otherGrouping.numThoughts.compareTo(numThoughts);
			default:
				return super.compareTo(otherGrouping);
			}
		} else {
			throw new RuntimeException(
					"Statistical Grouping can only be compared to other Statistical Groupings.");
		}
	}
}
